/**
 * @file navigation-context.tsx
 * @description 导航状态的上下文管理器
 */

'use client'

import { createContext, useContext, useState, ReactNode } from 'react'
import { useSelectedLayoutSegment } from 'next/navigation'

export type NavigationSection = 'explore' | 'knowledge' | 'admin'

interface NavigationContextType {
  // 当前导航区域
  currentSection: NavigationSection
  // 设置当前导航区域
  setCurrentSection: (section: NavigationSection) => void
  // 当前区域的子路径
  currentPath: string
  // 设置当前区域的子路径
  setCurrentPath: (path: string) => void
}

const NavigationContext = createContext<NavigationContextType | undefined>(undefined)

export function NavigationProvider({ children }: { children: ReactNode }) {
  const selectedSegment = useSelectedLayoutSegment()
  const [currentSection, setCurrentSection] = useState<NavigationSection>(
    (selectedSegment as NavigationSection) || 'explore'
  )
  const [currentPath, setCurrentPath] = useState<string>('')

  const value = {
    currentSection,
    setCurrentSection,
    currentPath,
    setCurrentPath
  }

  return (
    <NavigationContext.Provider value={value}>
      {children}
    </NavigationContext.Provider>
  )
}

export function useNavigation() {
  const context = useContext(NavigationContext)
  if (context === undefined) {
    throw new Error('useNavigation must be used within a NavigationProvider')
  }
  return context
} 